package com.acme.bankapp.domine.account;

import java.io.Serializable;

import com.acme.bankapp.exceptions.NoEnoughFundsException;

@SuppressWarnings("serial")
public abstract class AbstractAccount implements Account, Serializable {

    private int id;
    private double balance;
    private String type;

    public AbstractAccount(final double amount)
            throws IllegalArgumentException {
        this.id = getUniqueAccountNumber();
        if (amount < 0) {
            throw new IllegalArgumentException("Balance can not be negative");
        }
        balance = amount;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(final double balance) {
        this.balance = balance;
    }

    public int getId() {
        return id;
    }

    public void deposit(final double amount) {
        if (amount < 0) {
            System.out.println("Amount deposit can not be negative");
            return;
        }
        balance += amount;
    }

    public long decimalValue() {
        return Math.round(balance);
    }

    public String getType() {
        return type;
    }

    public void setType(final String type) {
        this.type = type;
    }

    public abstract void withdraw(final double amount)
            throws NoEnoughFundsException;

    @Override
    public boolean equals(final Object o) {
        // Comparison of business keys (id) enough here
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        AbstractAccount that = (AbstractAccount) o;

        if (this.getId() != that.getId()) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = id;
        temp = balance != +0.0d ? Double.doubleToLongBits(balance) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    public static int getUniqueAccountNumber() {
        return (int) System.currentTimeMillis();
    }
}
